package com.mayuanfei.springboot16.controller;

import cn.hutool.core.io.IoUtil;
import com.mayuanfei.springboot16.config.MinioProperties;
import io.minio.*;
import io.minio.http.Method;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * TODO 类描述
 *
 * @author 老马
 * @date 2024-12-20 13:07
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/minio")
public class MinioController {

    private final MinioClient minioClient;
    private final MinioProperties minioProperties;

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        try {
            PutObjectArgs objectArgs = PutObjectArgs.builder()
                    .stream(file.getInputStream(), file.getSize(), -1)
                    .bucket(minioProperties.getBucketName())
                    .object("ccc/" + file.getOriginalFilename())
                    .contentType(file.getContentType())
                    .build();
            minioClient.putObject(objectArgs);
            return "上传成功";
        }catch (Exception e) {
            log.error("上传文件失败", e);
            return "上传失败";
        }
    }

    @RequestMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try (GetObjectResponse objectResponse = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(minioProperties.getBucketName())
                        .object(fileName)
                        .build())) {
            // 设置响应头
            response.setContentType(objectResponse.headers().get("Content-Type"));
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
            // 写入响应流
            IoUtil.copy(objectResponse, response.getOutputStream());
            response.getOutputStream().flush();
        } catch (Exception e) {
            log.error("下载文件失败", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取文件访问的完整URL
     */
    @RequestMapping("/fileUrl")
    public String getFileUrl(@RequestParam("fileName") String fileName) {
        try {
            String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .object(fileName)
                    .method(Method.GET)
                    .build());
            log.info("文件访问URL: {}", url);
            return url;
        } catch (Exception e) {
            log.error("获取文件URL失败", e);
            return "获取文件URL失败";
        }
    }

    /**
     * 删除文件
     */
    @RequestMapping("/delete")
    public String deleteFile(@RequestParam("fileName") String fileName) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                    .bucket(minioProperties.getBucketName())
                    .object(fileName)
                    .build());
            log.info("文件 {} 删除成功", fileName);
            return "删除成功";
        } catch (Exception e) {
            log.error("删除文件失败", e);
            return "删除失败";
        }
    }

}